package pers.qianyu.module.core.domain.image.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pers.qianyu.module.core.comm.BaseConverter;
import pers.qianyu.module.core.domain.image.dto.ImageDTO;
import pers.qianyu.module.core.domain.image.entity.ImagePO;
import pers.qianyu.module.core.domain.image.enums.ImageStatusEnum;
import pers.qianyu.module.core.domain.image.query.ImageQuery;
import pers.qianyu.module.core.domain.image.vo.ImageVO;
import pers.qianyu.module.image.cache.DailyRankCache;

/**
 * @author mizzle rain
 * @date 2020-11-05 16:00
 */
@Component
public class ImageConverter extends BaseConverter {
    @Autowired
    private DailyRankCache dailyRankCache;

    public ImagePO convertToPO(ImageDTO imageDTO) {
        ImagePO imagePO = new ImagePO();
        imagePO.setUrl(imageDTO.getUrl());
        imagePO.setTitle(imageDTO.getTitle());
        imagePO.setDescription(imageDTO.getDescription());
        imagePO.setLikes(0L);
        imagePO.setViews(0L);
        imagePO.setComments(0L);
        imagePO.setFavorites(0L);
        imagePO.setStatus(0);
        imagePO.setCreatedBy(imageDTO.getCreatedBy());
        imagePO.setUpdatedBy(imagePO.getCreatedBy());
        return imagePO;
    }

    public ImageVO convertToVO(ImagePO imagePO, String[] labels) {
        ImageVO imageVO = new ImageVO();
        imageVO.setUrl(imagePO.getUrl());
        imageVO.setTitle(imagePO.getTitle());
        imageVO.setDescription(imagePO.getDescription());
        imageVO.setLabel(labels);
        imageVO.setLikes(imagePO.getLikes());
        imageVO.setComments(imagePO.getComments());
        imageVO.setFavorites(imagePO.getFavorites());
        imageVO.setStatus(ImageStatusEnum.getValueFromCode(imagePO.getStatus()));
        imageVO.setRemark(imagePO.getRemark());

        Long score = dailyRankCache.score(imagePO.getId());
        imageVO.setViews(imagePO.getViews() + score);
        entityToVO(imagePO, imageVO);
        return imageVO;
    }

    public ImagePO convertToPO(ImageQuery imageQuery) {
        ImagePO imagePO = new ImagePO();
        imagePO.setTitle(imageQuery.getTitle());
        imagePO.setDescription(imageQuery.getDescription());
        imagePO.setCreatedBy(imageQuery.getCreatedBy());
        imagePO.setUpdatedBy(imageQuery.getUpdatedBy());
        imagePO.setStatus(imageQuery.getStatus());
        return imagePO;
    }
}
